package eproject.mm.com.projectsupervision.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.xiasuhuei321.loadingdialog.view.LoadingDialog;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import eproject.mm.com.projectsupervision.PrjApplication;
import eproject.mm.com.projectsupervision.R;
import eproject.mm.com.projectsupervision.util.HttpUtil;
import eproject.mm.com.projectsupervision.widget.IconFontTextview;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class QuantityActivity extends Activity {
    private static final int RequestCode_ProjectSelect = 10005;

    String sPrjId = "";
    TextView tv_PrjName;
    EditText et_val_01,et_val_02,et_val_03,et_val_04,et_val_05,et_val_06,et_val_07,et_val_08,et_val_09;
    IconFontTextview iftv_back;

    private OkHttpClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setContentView(R.layout.activity_quantity);

        tv_PrjName = (TextView)findViewById(R.id.tv_prjName);
        et_val_01 = (EditText)findViewById(R.id.et_val_01);
        et_val_02 = (EditText)findViewById(R.id.et_val_02);
        et_val_03 = (EditText)findViewById(R.id.et_val_03);
        et_val_04 = (EditText)findViewById(R.id.et_val_04);
        et_val_05 = (EditText)findViewById(R.id.et_val_05);
        et_val_06 = (EditText)findViewById(R.id.et_val_06);
        et_val_07 = (EditText)findViewById(R.id.et_val_07);
        et_val_08 = (EditText)findViewById(R.id.et_val_08);
        et_val_09 = (EditText)findViewById(R.id.et_val_09);

        iftv_back = (IconFontTextview)findViewById(R.id.iftv_back);
        iftv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        client = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(10, TimeUnit.SECONDS)
                .build();
    }

    public  void onProjectSelectClick(View view){
        Intent intent = new Intent(QuantityActivity.this, ProjectSelectActivity.class);
        //poss = position;
        startActivityForResult(intent, RequestCode_ProjectSelect);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //if(data!=null){
        if(requestCode == RequestCode_ProjectSelect && data != null){
            Bundle bundle = data.getExtras();
            String id = bundle.getString("id");
            String name = bundle.getString("name");

            sPrjId = id;
            tv_PrjName.setText(name);

            final LoadingDialog loadingDialog = new LoadingDialog(this);
            loadingDialog.setLoadingText("加载中...").show();

            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        String resultStr = HttpUtil.get("/getQuantityDataMap?prjid="+sPrjId);

                        handler.sendMessage(handler.obtainMessage(1, resultStr));
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                loadingDialog.close();
                            }
                        });
                    } catch (Exception e) {
                        final String s = e.getMessage();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                loadingDialog.close();
                                Toast.makeText(QuantityActivity.this,s,Toast.LENGTH_LONG);
                            }
                        });
                    }
                }
            }).start();
        }

    }
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    String str = (String)msg.obj;
                    Gson gson=new Gson();
                    try{
                        Map resultMap = gson.fromJson(str, Map.class);
                        if (resultMap != null && (boolean) resultMap.get("success")) {
                            List data = (List)resultMap.get("data");
                            if(data != null && data.size()>0){
                                for(int i=0;i<data.size();i++){
                                    Map item = (Map)data.get(i);
                                    String code = (String)item.get("idxcode");
                                    String val = (String)item.get("val");
                                    val = (val==null)?"":val;
                                    if(code.equals("01")){
                                        et_val_01.setText(val);
                                    }else if(code.equals("02")){
                                        et_val_02.setText(val);
                                    }else if(code.equals("03")){
                                        et_val_03.setText(val);
                                    }else if(code.equals("04")){
                                        et_val_04.setText(val);
                                    }else if(code.equals("05")){
                                        et_val_05.setText(val);
                                    }else if(code.equals("06")){
                                        et_val_06.setText(val);
                                    }else if(code.equals("07")){
                                        et_val_07.setText(val);
                                    }else if(code.equals("08")){
                                        et_val_08.setText(val);
                                    }else if(code.equals("09")){
                                        et_val_09.setText(val);
                                    }
                                }
                            }else{
                                et_val_01.setText("");
                                et_val_02.setText("");
                                et_val_03.setText("");
                                et_val_04.setText("");
                                et_val_05.setText("");
                                et_val_06.setText("");
                                et_val_07.setText("");
                                et_val_08.setText("");
                                et_val_09.setText("");
                            }
                        }
                    }catch (Exception e){}
            }
        }
    };
    public void onSubmit(View view) {
        if (sPrjId.equals("")) {
            Toast.makeText(this, "请选择项目", Toast.LENGTH_SHORT).show();
            return;
        }

        final LoadingDialog loadingDialog = new LoadingDialog(this);
        loadingDialog.setLoadingText("加载中...").show();

        MultipartBody.Builder builder = new MultipartBody.Builder();

        builder.addFormDataPart("prjid",sPrjId);
        builder.addFormDataPart("val1",et_val_01.getText().toString());
        builder.addFormDataPart("val2",et_val_02.getText().toString());
        builder.addFormDataPart("val3",et_val_03.getText().toString());
        builder.addFormDataPart("val4",et_val_04.getText().toString());
        builder.addFormDataPart("val5",et_val_05.getText().toString());
        builder.addFormDataPart("val6",et_val_06.getText().toString());
        builder.addFormDataPart("val7",et_val_07.getText().toString());
        builder.addFormDataPart("val8",et_val_08.getText().toString());
        builder.addFormDataPart("val9",et_val_09.getText().toString());

        RequestBody requestBody = builder.build();

        Request.Builder okbuilder = new Request.Builder();
        okbuilder.addHeader("authenticationToken", PrjApplication.getInstance().getAuthenticationToken());
        Request requestPostFile = okbuilder
                .url("http://"+ PrjApplication.getInstance().getServerIp() +":"+PrjApplication.getInstance().getServerPort()+"/"+PrjApplication.getInstance().getPreFixPath()+"/quantitySubmit")
                .post(requestBody)
                .build();
        client.newCall(requestPostFile).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                loadingDialog.close();
            }

            @Override
            public void onResponse(Call call, final Response response) throws IOException {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        loadingDialog.close();
                        QuantityActivity.this.finish();
                    }
                });
            }
        });
    }
}
